package com.wm.task.util;

import java.sql.*;

public class DbHelper {
    //private static String driverClassName = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/demosys?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
    private static String user = "root";
    private static String pass = "123456";

//    static{
//        try {
//            Class.forName(driverClassName);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//
//    }
    public static Connection dbConn(){
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(url,user,pass);
            return connection;
        } catch (SQLException e) {
            //throw new RuntimeException(e);
        }
        return null;
    }
    public static void upDate(String sql,Object ... params){
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(url,user,pass);
            PreparedStatement ps = connection.prepareStatement(sql);
            for (int i = 0; params!=null && i < params.length; i++) {
                ps.setObject(i+1,params[i]);
            }
            ps.executeUpdate();
        } catch (SQLException e) {
            //throw new RuntimeException(e);
        }
    }




}
